home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / mathematica / tex-mma.tar_z / tex-mma / unix-tex-mma.el < prev   
Lisp/Scheme  |  1991-05-08  |  7KB  |  215 lines

  1. ;; File unix-tex-mma.el
  2. ;; UNIX shell interface to TeX/Mathematica
  3. ;; Written 4/11/1991 by Dan Dill dan@chem.bu.edu
  4.  
  5. (defconst unix-tex-mma-version-string
  6.   "TeX/Mathematica UNIX shell interface Version 1 <Thur 11 April 1991>"
  7.   "String describing this version of TeX/Mathematica UNIX shell interface.")
  8.  
  9. ;; Copyright (C) 1991 Dan Dill
  10. ;; This is part of TeX/Mathematica
  11. ;;
  12. ;; TeX/Mathematica is distributed in the hope that it will be useful, but
  13. ;; WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  14. ;; anyone for the consequences of using it or for whether it serves any
  15. ;; particular purpose or works at all, unless he says so in writing.
  16. ;;
  17. ;; Everyone is granted permission to copy, modify and redistribute this
  18. ;; tex-mma package, provided:
  19. ;;  1.  All copies contain this copyright notice.
  20. ;;  2.  All modified copies shall carry a prominant notice stating who
  21. ;;      made the last modification and the date of such modification.
  22. ;;  3.  No charge is made for this software or works derived from it.  
  23. ;;      This clause shall not be construed as constraining other software
  24. ;;      distributed on the same medium as this software, nor is a
  25. ;;      distribution fee considered a charge.
  26. ;;
  27. ;; Portions of this package were adapted from GNU Emacs.
  28. ;;
  29. ;; Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc. 
  30. ;; 
  31. ;; GNU Emacs is distributed in the hope that it will be useful,
  32. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  33. ;; accepts responsibility to anyone for the consequences of using it
  34. ;; or for whether it serves any particular purpose or works at all,
  35. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  36. ;; License for full details.
  37. ;;
  38. ;; Everyone is granted permission to copy, modify and redistribute
  39. ;; GNU Emacs, but only under the conditions described in the
  40. ;; GNU Emacs General Public License.   A copy of this license is
  41. ;; supposed to have been given to you along with GNU Emacs so you
  42. ;; can know your rights and responsibilities.  It should be in a
  43. ;; file named COPYING.  Among other things, the copyright notice
  44. ;; and this notice must be preserved on all copies.
  45.  
  46.  
  47. ;; Developed with GNU Emacs version 18.55
  48.  
  49.  
  50. (defun unix-tex-mma-hook ()
  51.   "Hook for TeX/Mathematica."
  52.   ; Put any setup processing you need to do here.  You don't have to
  53.   ; do any, but this function must still be defined.
  54.   )
  55.  
  56. (defconst unix-tex-mma-cell-alist
  57.   '(
  58.     ("buffer-go" . unix-buffer-go )
  59.     ("recenter" . unix-recenter)
  60.     ("replace" . unix-replace)
  61.     ("send" . unix-send)
  62.     ("send-cell" . unix-send-cell)
  63.     ("show" . unix-show)
  64.     ("update" . unix-update)
  65.     ))
  66.  
  67. (defun unix-start ()
  68.   "Start up UNIX shell mode, if necessary."
  69.   (require 'shell)
  70.   (if (unix-start-p)
  71.       nil
  72.     (let ((home-buffer (current-buffer)))
  73.       (message "Starting UNIX shell...")
  74.       (pop-to-buffer unix-process-buffer)
  75.       (shell)
  76.       ; Wait until startup is done
  77.       (while (not (looking-at shell-prompt-pattern))
  78.     (sit-for 1)
  79.     (goto-char (point-max))
  80.     (beginning-of-line))
  81.       (goto-char (point-max))
  82.       (pop-to-buffer home-buffer)
  83.       (message "")
  84.       )))
  85.  
  86. (defconst unix-process-name "shell"
  87.   "The name of the inferior unix process.")
  88. (defconst unix-process-buffer "*shell*"
  89.   "The name of the shell-mode buffer running UNIX shell.")
  90. (defconst unix-process-string "math" ; For now
  91.   "String to pass to Unix exec fucntion to start UNIX shell.")
  92.  
  93. (defun unix-start-p ()
  94.   (if (get-buffer-process unix-process-buffer)
  95.       t
  96.     nil
  97.     ))
  98.  
  99. (defun unix-buffer-go ()
  100.   "Go to UNIX shell buffer."
  101.   (interactive)
  102.   (unix-start)
  103.   (unix-pop-to-buffer)
  104.   )
  105.  
  106. (defun unix-recenter ()
  107.   "Place UNIX shell input prompt at top of window."
  108.   (interactive)
  109.   (unix-start)
  110.   (let ((home-buffer (current-buffer)))
  111.     (unix-pop-to-buffer)
  112.     (set-window-start (selected-window) last-input-end)
  113.     (goto-char last-input-end)
  114.     (pop-to-buffer home-buffer)
  115.     ))
  116.  
  117. (defun unix-show ()
  118.   "Make last UNIX shell output visible."
  119.   (unix-start)
  120.   (let ((home-buffer (current-buffer)))
  121.     (unix-pop-to-buffer)
  122.     (goto-char last-input-end)
  123.     (recenter)
  124.     (pop-to-buffer home-buffer)
  125.     ))
  126.  
  127. (defun unix-pop-to-buffer ()
  128.   (pop-to-buffer (process-buffer (get-process unix-process-name)))
  129.   )
  130.  
  131. (defun unix-send ()
  132.   "Send UNIX statements containing point to UNIX shell.
  133. Statements are delimited by blank lines."
  134.   (interactive)
  135.   (unix-start)
  136.   (let ((start (tex-mma-math-start))
  137.     (end (tex-mma-math-end))
  138.     (home-buffer (current-buffer)))
  139.     (unix-pop-to-buffer)
  140.     (goto-char (point-max))
  141.     (insert-buffer-substring home-buffer start end)
  142.     ))
  143.  
  144. (defun unix-send-cell ()
  145.   "Send input to UNIX shell.  Point must be in a unix cell."
  146.   (interactive)
  147.   (if (not (tex-mma-cell-p "unix"))
  148.       (error "Not in unix cell"))
  149.   (unix-start)
  150.   (let ((home-buffer (current-buffer))
  151.     start end)
  152.     (save-excursion
  153.       (goto-char (tex-mma-cell-start "unix"))
  154.       (setq start (point))
  155.       (if (not (setq end (tex-mma-output-p "unix")))
  156.       (setq end (tex-mma-cell-end "unix"))))
  157.     (unix-pop-to-buffer)
  158.     (goto-char (point-max))
  159.     (insert-buffer-substring home-buffer start end)
  160.     (shell-send-input)
  161.     (pop-to-buffer home-buffer)
  162.     ))
  163.  
  164. (defun unix-replace ()
  165.   "Replace output (if any) with last UNIX shell result.
  166. Point must be in unix cell.
  167. Output assumed to follow input, separated by a blank line."
  168.   (interactive)
  169.   (if (not (tex-mma-cell-p "unix"))
  170.       (error "Not in UNIX cell"))
  171.   (unix-start)
  172.   (save-excursion
  173.     (tex-mma-delete-output "unix")
  174.     (unix-get)
  175.     ))
  176.  
  177. (defun unix-update ()
  178.   "Send input to UNIX shell and optionally replace output with result.
  179. Point must be in unix cell.
  180. Output assumed to follow input, separated by a blank line."
  181.   (interactive)
  182.   (if (not (tex-mma-cell-p "unix"))
  183.       (error "Not in UNIX cell"))
  184.   (unix-send-cell)
  185.   ; We need to wait for UNIX output anyhow, so we might as well ask
  186.   (if (y-or-n-p "Replace output? ")
  187.       (unix-replace)
  188.     t
  189.     ))
  190.  
  191. (defun unix-get ()
  192.   "Insert last output from UNIX shell.
  193. Assumes point in cell.  Output inserted at end of cell."
  194.   (unix-start)
  195.   (let ((tex-mma-process (get-process unix-process-name)))
  196.     (let ((tex-mma-buffer (process-buffer tex-mma-process))
  197.       (home-buffer (current-buffer))
  198.       out-start out-end)
  199.       (pop-to-buffer tex-mma-buffer)
  200.       (goto-char last-input-end) ; First line of UNIX output
  201.       (setq out-start (point))     ; ..
  202.       (goto-char (point-max))     ; Last line
  203.       (beginning-of-line)     ; .. exclude next prompt
  204.       (setq out-end (point))
  205.       (goto-char (point-max))     ; Leave point at next prompt
  206.       (pop-to-buffer home-buffer)
  207.       (save-excursion ; Insert output before \end{mathematica}
  208.     (goto-char (tex-mma-cell-end "unix"))
  209.     (forward-line 1)     ; Insert blank line before output
  210.     (open-line 2)         ; ..
  211.     (forward-line 1)     ; ..
  212.     (insert-buffer-substring tex-mma-buffer out-start out-end)
  213.     (kill-line 1) ; Delete blank lines at end of output
  214.     ))))
  215.